/*
*********************************************************************************************************************************
*/
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID('MarketClose') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
BEGIN
	PRINT 'Droping procedure dbo.MarketClose'
	DROP PROCEDURE dbo.MarketClose
END
GO

PRINT 'Creating procedure dbo.MarketClose'
GO

CREATE PROCEDURE dbo.MarketClose
AS
BEGIN
	-- Start code added temporarily for LINQ (Auto-generated Type) 
	SET FMTONLY OFF;
	-- End code added temporarily

	/*
	OrderStatus  Active = 0, Pending = 1, Accepted = 2, Rejected = 3, Closed = 4, OnHold = 5
	*/
	
	UPDATE Ord SET Ord.[Status] = 4 -- Closed
	FROM dbo.[Order] Ord
	WHERE Ord.[Status] <> 5 -- OnHold

	/*
	TradeActivityStatus  Active = 0, Pending = 1, Completed = 2, Canceled = 3
	*/

	UPDATE TA SET TA.[Status] = 3 -- Trade Status  -> Canceled
	FROM dbo.[TradeActivity] TA
	WHERE TA.[Status] IN (0,1) -- Active, Pending 
END
GO